
#include "Storage.h"

char HwDatasBuff[CN_HW_BUFFER];

char AppDatasBuff[CN_HW_BUFFER];
#define CN_STORAGE_BUFFER_LENGTH_MIN      (1024*3)

int ProtocolBase::init(int length)
{
    char *in,*out;
    if(length < CN_STORAGE_BUFFER_LENGTH_MIN){
        length = CN_STORAGE_BUFFER_LENGTH_MIN
    }

    in = (char *)malloc(length);
    if(in == NULL){
        LOGOUT_E("ProtocolBase init Memory error!\n");
        return -1;
    }
    out = (char *)malloc(length);
    if(out == NULL){
        free(in);
        LOGOUT_E("ProtocolBase init Memory error 2!\n");
        return -1;
    }

    StorageInit(&mHwInStorage,in,length);
    StorageInit(&mHwOutStorage,out,length);
    mBufState = 1;
    return 0;
}

int ProtocolBase::uninit()
{
    if(mBufState == 1){
        free(mHwInStorage.buffer);
        free(mHwOutStorage.buffer);
        StorageClean(&mHwInStorage);
        StorageClean(&mHwOutStorage);
    }
}

int ProtocolBase::PutCarDatas(char *datas,int length)
{
    StorageAddDatas(&mHwInStorage,datas,length);
    return ++mCarStorageCount;
}

int ProtocolBase::GetCarDatas(char* buf,int length)
{
    return StorageGetDatas(&mHwInStorage,buf+count,length-count);
}

int ProtocolBase::PutAppDatas(char *datas,int length)
{
    StorageAddDatas(&mHwOutStorage,datas,length);
    return ++mAppStorageCount;
}

int ProtocolBase::GetAppDatas(char* buf,int length)
{
    int len,count = 0;
    while(1){
        len = StorageGetDatas(&mHwOutStorage,buf+count,length-count);
        if(len < 1){
            break;
        }
        count += len;
    }
    return count;
}

int ProtocolBase::start()
{
    return 0;
}

int ProtocolBase::stop()
{
    return 0;
}

